<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="25%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      label-position="right"
      class="noFlex"
    >
      <el-form-item label="编码" prop="dictsortCode">
        <el-input
          v-model="form.dictsortCode"
          clearable
          placeholder="字典编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="字典名称" prop="dictsortName">
        <el-input
          v-model="form.dictsortName"
          clearable
          placeholder="字典显示名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="父路径">
        <el-cascader
          v-model="form.pid"
          :options="dic"
          :props="dicProps"
          :show-all-levels="false"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增字典',
        edit: '编辑字典'
      },
      visible: false,
      isSaveing: false,
      form: {
        dictsortName: "",//分类名称
        dictsortCode: "",//字典编码
        pid: ""//上级分类id
      },
      rules: {
        dictsortCode: [
          { required: true, message: '请输入编码' }
        ],
        dictsortName: [
          { required: true, message: '请输入字典名称' }
        ]
      },
      dic: [],
      dicProps: {
        value: "id",
        label: "dictsortName",
        emitPath: false,
        checkStrictly: true
      }
    }
  },
  mounted () {
    this.getDic()
  },
  methods: {
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      return this
    },
    //获取字典列表
    async getDic () {
      var res = await this.$API.dictionary.getDictionaryList.get()
      this.dic = this.resetTree(res.data)
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.dictionary.addDictionary.post(this.form)
          } else if (this.mode == 'edit') {
            res = await this.$API.dictionary.updateDictionary.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData (data) {
      console.log(data)
      this.form.id = data.id
      this.form.dictsortName = data.dictsortName
      this.form.dictsortCode = data.dictsortCode
      this.form.pid = data.pid
    },

    // 初始化树
    resetTree (list, p_id) {
      return list.filter(item => p_id == undefined ? !item.pid : item.pid == p_id).map(e_item => {
        let obj = {
          id: e_item.id,
          pid: e_item.pid,
          dictsortCode: e_item.dictsortCode,
          dictsortName: e_item.dictsortName,
          children: this.resetTree(list, e_item.id)
        }
        return obj
      })

    }
  }
}
</script>

<style>
</style>
